package dao

import (
	"server/basic/config"
	"server/basic/model"
)

func CourseList(page int64, size int64) (course []model.Courses, err error) {
	page = page
	if page <= 0 {
		page = 1
	}

	pageSize := size
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	err = config.DB.Offset(int(offset)).Limit(int(pageSize)).Find(&course).Error
	if err != nil {
		return nil, err
	}
	return course, nil
}

func CourseListRecommend() (course []model.Courses, err error) {
	err = config.DB.Where("course_recommend=?", 1).Find(&course).Error
	if err != nil {
		return nil, err
	}
	return course, nil
}

func CourseUpdate(id int64) bool {
	err := config.DB.Model(&model.Courses{}).Where("id=?", id).Update("course_recommend", 1).Error
	if err != nil {
		return false
	}
	return true
}

func CourseLong() (course []model.Courses, err error) {
	err = config.DB.Where("course_recommend=?", 1).Find(&course).Error
	if err != nil {
		return nil, err
	}
	return course, nil
}
